package lab.dexter.bbsreader;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;

/**
 * @author wenpu
 *
 * Instances of subclasses should be FSMs, keep track of everything happened
 * to the list browser.
 */
public abstract class ListManager implements DataHandler, ListAdapter{
	
	private List<DataSetObserver> mObserverList;
	protected Context mContext;
	
	public ListManager(Context context, Intent intent) {
		mObserverList = new ArrayList<DataSetObserver>();
		mContext = context;
	}
	
	public boolean hasStableIds() {
		return false;
	}
	
	public void registerDataSetObserver(DataSetObserver observer) {
		mObserverList.add(observer);
	}
	
	public void unregisterDataSetObserver(DataSetObserver observer) {
		for (Iterator<DataSetObserver> it = mObserverList.iterator(); it.hasNext();) {
			DataSetObserver ob = it.next();
			if (ob.equals(observer)) {
				it.remove();
			}
		}
	}
	

	public boolean isEmpty() {
		return false;
	}

	/**
	 * notify the change of underling data, should be called whenever re-render is needed
	 */
	public void notifyDataChanged() {
		for (Iterator<DataSetObserver> it = mObserverList.iterator(); it.hasNext();) {
			DataSetObserver ob = it.next();
			ob.onChanged();
		}
	}
	
	/**
	 * called right after the manager is instantiated
	 */
	public String getTitleSuffix() {
		return "";
	}

	
	/**
	 * subclass should overload this method
	 */
	abstract public int update();
	
	/**
	 * 
	 * @param position
	 * @return true if update needs to be called
	 */
	abstract public boolean onItemClick(int position);
	
	
	/**
	 * @return true if there is context menu
	 */
	public boolean hasContextMenu() {
		return false;
	}
	
	public int getContextMenuResource() {
		return 0; // the default value may crash the activity
	}
	
	public void onContextItemSelected(MenuItem item) {
		// default behavior
	}
	
	public boolean hasBottomPanel() {
		return false;
	}
	
	/**
	 * @return Return a single view, which will be inserted into
	 * the panel of 
	 */
	public int getBottomPanelResource() {
		return 0;
	}
	
	public int[] getBottomPanelMembers() {
		return null;
	}
	
	public boolean onClickOnBottomPanel(View view) {
		return false;
	}
}
